
//
//  HomeTableViewCell.swift
//  swiftImitationWeibo
//
//  Created by 杨果果 on 16/7/13.
//  Copyright © 2016年 yang. All rights reserved.
//

import UIKit
import SnapKit
let HomeCellMargin: CGFloat = 10
class HomeTableViewCell: UITableViewCell {
    
    var toolbarTopConstraint: Constraint?
    
    var statueViewModel: StatueViewModel? {
        didSet {
            originalView.statusViewModel = statueViewModel
            //卸载约束
            toolbarTopConstraint?.uninstall()
            //是不是有转发微博栏
            if statueViewModel?.statues?.retweeted_status != nil{
                retweetView.statuesViewModel = statueViewModel
                retweetView.hidden = false
                toolBarView.snp_updateConstraints(closure: { (make) -> Void in
                    self.toolbarTopConstraint = make.top.equalTo(retweetView.snp_bottom).constraint
                })
            } else {
            //没有转发微博数据， 隐藏转发微博视图
                retweetView.hidden = true
            toolBarView.snp_updateConstraints(closure: { (make) -> Void in
                    self.toolbarTopConstraint = make.top.equalTo(originalView.snp_bottom).constraint
                })
            }
            toolBarView.statuesViewModel = statueViewModel
        }
    }
    
    
    
    
    //懒加载view
    private lazy var originalView: StatusOriginalView = StatusOriginalView()
    private lazy var retweetView: StatusRetweetView = StatusRetweetView()
    private lazy var toolBarView: StatusToolBar = StatusToolBar()
 
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setupUI()
    }
    

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    private func setupUI() {
        contentView.backgroundColor = UIColor(white: 0.95, alpha: 1)

        //添加约束
        contentView.addSubview(originalView)
        contentView.addSubview(retweetView)
        contentView.addSubview(toolBarView)
        
        
        originalView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(contentView).offset(8)
            make.leading.equalTo(contentView)
            make.trailing.equalTo(contentView)
            
        }
        retweetView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(originalView.snp_bottom)
            make.leading.equalTo(originalView)
            make.trailing.equalTo(originalView)
        }
        toolBarView.snp_makeConstraints { (make) -> Void in
              self.toolbarTopConstraint = make.top.equalTo(retweetView.snp_bottom).offset(HomeCellMargin).constraint
            make.leading.equalTo(contentView)
            make.trailing.equalTo(contentView)
            make.height.equalTo(35)
        }
        contentView.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(toolBarView)
            make.top.equalTo(self)
            make.leading.equalTo(self)
            make.trailing.equalTo(self)
            
        }
    }
}
